package com.shadt.cloud.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shadt.cloud.dao.BranchMDao;
import com.shadt.cloud.entity.CloudBranch;
import com.shadt.cloud.service.BranchMService;
import com.shadt.cloud.vo.BranchVo;

/**
 * 分平台权限业务逻辑层接口
 * 
 * @author Maogc
 *
 */
@Service
public class BranchMServiceImpl implements BranchMService {
	@Autowired
	BranchMDao dao;
	@Override
	public boolean update(int branch_id,int id,int status) {
		int i = dao.update(branch_id,id,status);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
	public BranchVo login(String phone, String password) {
		CloudBranch b = (CloudBranch) dao.get("from CloudBranch where userphone='"+phone+"' and password='"+password+"'");
		BranchVo vo = new BranchVo();
		if (null != b && b.getStatus() != -1) {
			BeanUtils.copyProperties(b, vo);
		} else {
			return null;
		}
		return vo;
	}
	
	public String adminLogin(String name, String password) {
		List<Map<String, Object>> list = dao.login(name, password);
		String adminjson = null;
		if(list.size()>0){
			adminjson = JSON.toJSONString(list.get(0));
		}
		return adminjson;
	}
	@SuppressWarnings("rawtypes")
	public String channelList(int page,int pnum,String type,String search){
		StringBuffer sb = new StringBuffer();
		StringBuffer sb1 = new StringBuffer();
		sb.append("select cc,cb.username,cb.name FROM CloudBranchChannel cc,CloudBranch cb where cc.branchid=cb.id");
		sb1.append("select count(*) from CloudBranchChannel cc");
		if(!"".equals(search) && search!=null){
			if("1".equals(type)){
				sb.append(" AND cc.key LIKE '%"+search+"%'");
				sb1.append(" where key LIKE '%"+search+"%'");
			}else{
				sb.append(" AND cb.username LIKE '%"+search+"%'");
				sb1.append(" ,CloudBranch cb where cc.branchid=cb.id AND cb.username LIKE '%"+search+"%'");
			}
		}
		List list = dao.find(sb.toString(), page, pnum);
		long count = dao.count(sb1.toString());
		String chnarr=null;
		if(list.size()>0){
			chnarr = JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect);
			return chnarr+","+count;
		}else{
			return null;
		}
	}
}
